package beer.coder.walawala.gui.hook;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.dispatcher.DefaultDispatchService;

/**
 * Windows Hook.
 *
 * @author amath0312
 * @version 1.0
 * @date 2019-10-07 00:32
 */
public class GlobalHookUtil {

  /**
   * 初始化全局Hook，默认关闭日志输出.
   */
  public static void init() {
    try {
      disableLog();
      GlobalScreen.registerNativeHook();
    } catch (NativeHookException e) {
      e.printStackTrace();
      System.exit(-1);
    }
    GlobalScreen.setEventDispatcher(new DefaultDispatchService());
  }

  public static void destroy() {
    try {
      GlobalScreen.unregisterNativeHook();
    } catch (NativeHookException e) {
      e.printStackTrace();
      System.exit(-1);
    }
  }

  public static void disableLog() {
    Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
    logger.setLevel(Level.OFF);
    logger.setUseParentHandlers(false);
  }

  public static void enableAllLog() {
    Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
    logger.setLevel(Level.ALL);
    logger.setUseParentHandlers(false);
  }

  public static void enableWarnLog() {
    Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
    logger.setLevel(Level.WARNING);
    logger.setUseParentHandlers(false);
  }

}
